home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / RANDOM.ZIP / RANDOM.TXT
Encoding:
Text File  |  1996-02-17  |  13.4 KB  |  257 lines

  1.             SUGGESTIONS FOR RANDOM NUMBER GENERATION IN SOFTWARE
  2.  
  3.                    An RSA Data Security Engineering Report
  4.                                 Tim Matthews
  5.                           Revised December 15, 1995
  6.  
  7. Abstract
  8.  
  9. The generation of random numbers is critical to cryptographic systems.
  10. Symmetric ciphers such as DES, RC2, and RC5 all require a randomly selected
  11. encryption key. Public-key algorithms ù like RSA, Diffie-Hellman, and DSA ù
  12. begin with randomly generated values when generating prime numbers. At a
  13. higher level, SSL and other cryptographic protocols use random challenges in
  14. the authentication process to foil replay attacks.
  15.  
  16. But truly random numbers are difficult to come by in software-only
  17. solutions, where electrical noise and sources of hardware randomness are not
  18. available (or at least not convenient). This poses a challenge for software
  19. developers implementing cryptography. There are methods, however, for
  20. generating sufficiently random sequences in software that can provide an
  21. adequate level of security. This note offers suggestions on generating
  22. random numbers in software, along with a bit of background on random
  23. numbers.
  24.  
  25. Random vs. Pseudo-Random Numbers
  26.  
  27. What is a truly random number? The definition can get a bit philosophical.
  28. Knuth speaks of a sequence of independent random numbers with a specified
  29. distribution, each number being obtained by chance and not influenced by the
  30. other numbers in the sequence. Rolling a die would give such results. But
  31. computers are logical and deterministic by nature, and fulfilling Knuth's
  32. requirements is not something they were designed to do. So-called random
  33. number generators on computers actually produce pseudo-random numbers.
  34. Pseudo-random numbers are numbers generated in a deterministic way, which
  35. only appear to be random.
  36.  
  37. Most programming languages include a pseudo-random number generator, or
  38. "PRNG." This PRNG may produce a sequence adequate for a computerized version
  39. of blackjack, but it is probably not good enough to be used for
  40. cryptography. The reason is that someone knowledgeable in cryptanalysis
  41. might notice patterns and correlations in the numbers that get generated.
  42. Depending on the quality of the PRNG, one of two things may happen. If the
  43. PRNG has a short period, and repeats itself after a relatively short number
  44. of bits, the number of possibilities the attacker will need to try in order
  45. to deduce keys will be significantly reduced. Even worse, if the
  46. distribution of ones and zeros has a noticeable pattern, the attacker may be
  47. able to predict the sequence of numbers, thus limiting the possible number
  48. of resulting keys. An attacker may know that a PRNG will never produce 10
  49. binary ones in a row, for example, and not bother searching for keys that
  50. contain that sequence.
  51.  
  52. The detail of what makes a PRNG cryptographically "good" is a bit beyond the
  53. scope of this paper. Briefly stated, a PRNG must have a high degree of
  54. entropy, which is a measurement of randomness. Cryptographers use the word
  55. entropy a lot, so it is worth knowing. In a system that produces the same
  56. output each time, each bit is fixed, so there is no uncertainty, or zero
  57. entropy per bit. If every output is equally likely (i.e. truly random) then
  58. there is total uncertainty, or one bit's worth of entropy in each bit. A
  59. true random number generator, like a hardware device, will have maximum
  60. entropy. A good PRNG will have a high degree of entropy, making the output
  61. unguessable, which is the goal.
  62.  
  63. The first step in producing good random numbers in software, then, is to use
  64. a good PRNG. Important to note is that although the PRNG may produce
  65. statistically good looking output, it also has to withstand analysis to be
  66. considered strong. Since the one included with your compiler or operating
  67. system may or may not be, we recommend you don't use it. Instead, use a PRNG
  68. that has been verified to have a high degree of randomness. RSA's BSAFE
  69. toolkit uses the MD5 message digest function as a random number generator.
  70. BSAFE uses a counter that is digested with MD5. The strength of this
  71. approach relies on MD5 being a one-way function - from the random output
  72. bytes it is difficult to determine the counter, and hence the other output
  73. bytes remain secure. Similar generators can be constructed with other hash
  74. functions, eg. SHA1.
  75.  
  76. The Seed
  77.  
  78. The second step in producing good random numbers is providing a random seed.
  79. A good PRNG like BSAFE's will produce a sequence that is sufficiently random
  80. for cryptographic operations, with one catch: it needs to be properly
  81. initialized, or "seeded". Using a bad seed (or no seed at all) is a common
  82. flaw in poorly implemented cryptographic systems. A PRNG will always
  83. generate the same output if started with the same seed. If you are using MD5
  84. with the time of day as the seed, for example, an attacker has a high
  85. likelihood of re-creating your sequence of pseudo-random bytes by guessing
  86. the exact seeding time. Once he has the pseudo-random bytes, he can
  87. re-create your keys. The security issue becomes one of making sure an
  88. attacker cannot determine your seed.
  89.  
  90. You may be wondering why use a random number generator to generate random
  91. bytes, if to use it, you need to first generate random bytes. Seeding is a
  92. bootstrap operation. Once done, generating subsequent keys will be more
  93. efficient. Another important point is that the information collected for the
  94. seed does not need to be truly random, but unguessable and unpredictable.
  95. Once the seed is fed into MD5, the output becomes pseudo-random. If
  96. attackers cannot guess or predict seeds, they will be unable to predict the
  97. output.
  98.  
  99. There are two aspects to a random seed: quantity and quality. They are
  100. related. The quality of a random seed refers to its entropy. Since the
  101. quality may vary, it is a good idea to account for this with quantity.
  102. Sufficient quantity makes it impractical for an attacker to exhaustively try
  103. all likely seed values. Let's start with quality.
  104.  
  105. The following is a list of potential sources for building the initial seed
  106. pool. External random events are the best, but harder to get than variable
  107. or unique information. Sources that are variable, while not random, are very
  108. difficult for an attacker to guess. Quantities that are unique to a system
  109. are also hard to guess and usable if more bytes are needed.
  110.  
  111.            System Unique       Variable and       External Random
  112.                                Unguessable
  113.          Configuration     Contents of screen   Cursor position
  114.          files             Date and Time        with time
  115.          Drive             High resolution      Keystroke timing
  116.          configuration     clock samples        Microphone input
  117.          Environment                            (with microphone
  118.          strings           Last key pressed     connected)
  119.                            Log file blocks      Mouse click timing
  120.                            Network statistics   Mouse movement
  121.                            Process statistics   Memory statistics
  122.                            Program counter for  Video input
  123.                            other processes or
  124.                            threads
  125.  
  126.                                   [Image]
  127.  
  128. Table 1 Seed Sources
  129.  
  130. In general, collect as much external random information as possible.
  131. Supplement this with sources from the two other columns if more bytes are
  132. needed. Using a composite of many items makes the attacker's task more
  133. difficult. In an application where several keys will be generated, it may
  134. make sense to collect enough seed bytes for multiple keys, even before the
  135. first is generated. Be careful of information that moves across a network
  136. that could be intercepted by a dedicated attacker. Mouse movements on
  137. X-terminals, for example, may be available to anyone listening on the wire.
  138.  
  139. Now we get to the issue of quantity. A developer cannot assume that all of
  140. the bits collected are truly random, so a useful rule of thumb is to assume
  141. that for every byte of data collected at random, there is one bit of
  142. entropy. This is a bit conservative, but cryptographers are conservative by
  143. nature. To illustrate this rule of thumb, take the example of user
  144. keystrokes, which many consider to be a good source of randomness. Assuming
  145. ASCII keystrokes, bit 7 will always be zero. Many of the letters can be
  146. predicted: they will probably all be lowercase, and will often alternate
  147. between left and right hand. Analysis has shown that there is only one bit
  148. per byte of entropy per keystroke.
  149.  
  150. To guard against this kind of analysis, the idea is to collect one byte of
  151. seed for each bit required. This information will be fed into the PRNG to
  152. produce the first random output.
  153.  
  154. As an example, if the seed will be used to produce a random symmetric
  155. encryption key, the number of random bytes in the seed should at least equal
  156. the number of effective bits in the key. In the case of DES, this would be
  157. 56 random bits culled from a seed pool of 56 bytes. Any less and the number
  158. of possible starting keys is reduced from 256 to something smaller, reducing
  159. the amount of effort required by an attacker in searching the seed space by
  160. brute force. Attacks like this have recently been widely publicized on the
  161. Internet and in the press. For public-key algorithms, the goal is to make
  162. searching for the seed at least as difficult as the hard mathematical
  163. problem at their core. This will discourage attackers from searching for
  164. seeds instead of attacking problems like factoring composite numbers and
  165. calculating discreet logarithms. A seed of 128 bits (taken from a seed pool
  166. of 128 bytes) should be more than enough for the modulus sizes being used
  167. today.
  168.  
  169. One last thing that should be mentioned is updating the seed, or
  170. "re-seeding." It makes sense to allow an application to add seed bits as
  171. they become available. User events often provide additional sources of
  172. randomness, but obviously have not taken place when an application starts.
  173. These should be included as they occur. Re-seeding also frustrates attackers
  174. trying to find the seed state using a brute force attack. Since the seed
  175. will be change, say, every thirty seconds, the seed state becomes a moving
  176. target and makes the brute force attack infeasible. The idea is to take the
  177. existing seed and mix it together with the new information as it becomes
  178. available.
  179.  
  180. Example
  181. A brief example is in order. The diagram below illustrates how functions in
  182. BSAFE would be used to generate random keying material.
  183.  
  184. [Figure 1  Random Seed Process]
  185.  
  186. The first step is to supply the pool of random seed bytes. Let's assume that
  187. the application needs a random 80-bit RC2 key. Using the rule of thumb that
  188. one byte of data yields one bit of randomness, a minimum of 80 bytes will be
  189. needed for the pool. This pool would be gathered from the sources listed in
  190. Table 1. The B_RandomUpdate function in BSAFE takes the seed pool and runs
  191. it through the MD5 message digest algorithm to create the state.
  192.  
  193. The state is then used by the function B_GenerateRandomBytes, which runs it
  194. through MD5 to produce the key. This is the key that would be used for RC2.
  195. As an added measure, BSAFE automatically advances the state after random
  196. bytes are generated.
  197.  
  198. Notice the arrow labeled 'update' within B_RandomUpdate. This is where
  199. re-re-seeding is done. By calling B_RandomUpdate again, the state can be
  200. mixed with more seed information. Random information like key timing and
  201. mouse movement can be used here, along with changes in the system
  202. statistics, clock, and various files. Now the next RC2 key generated will be
  203. based on new seed material as well as the old.
  204.  
  205. Conclusion
  206.  
  207. Done properly, random number generation in software can provide the security
  208. necessary for most cryptographic systems. Using a good PRNG and choosing
  209. good seed material are the two critical points.
  210.  
  211. Developers may wish to create a set of routines to pull random and unique
  212. information from the operating system, which can then be used in any
  213. applications requiring cryptography. It may be desirable to save encrypted
  214. seed state for use in subsequent sessions.
  215.  
  216. Over time, as the need for cryptography in software increases, hardware and
  217. operating system vendors may provide more tools and hooks for random
  218. information. In the meantime, however, the techniques described can be used.
  219.  
  220. Further Reading
  221. For more information on random numbers and cryptography, take a look at the
  222. following:
  223.  
  224. Donald Eastlake, Steve Crocker, Jeff Schiller, "Randomness Recommendations
  225. for Security," IETF RFC 1750, 1994
  226.  
  227. Ian Goldberg and David Wagner, "Randomness and the Netscape Browser," Dr.
  228. Dobbs Journal, January 1996
  229.  
  230. Donald E. Knuth, The Art of Computer Programming: Seminumerical Algorithms,
  231. Addison-Wesley, Reading, MA, 1981
  232.  
  233. Colin Plumb, "Truly Random Numbers," Dr. Dobbs Journal, November 1994
  234.  
  235. RSA Data Security, Inc., "BSAFE User's Manual," Version 2.1, 1995
  236.  
  237. Bruce Schneier, Applied Cryptography, John Wiley & Sons, Inc., New York,
  238. 1995
  239.  
  240. Copyright ⌐ 1995 RSA Data Security, Inc.
  241. 910-907001-100-000-000
  242. ----------------------------------------------------------------------------
  243.                       Contact RSA Data Security, Inc.:
  244.                              100 Marine Parkway
  245.                               Redwood City, CA
  246.                                  94065-1031
  247.  
  248.                            phone: 415 _ 595 _ 8782
  249.                             fax: 415 _ 595 _ 1873
  250.                         Website: http://www.rsa.com/
  251.  
  252.        Website feedback or comments can be sent to : WEBMAVEN@RSA.COM
  253.  
  254.                   Copyright ⌐1996, RSA Data Security, Inc.
  255.                             All Rights Reserved.
  256.                    Last Updated: Friday, February 9, 1996
  257.